package com.lenin.omena.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.lenin.omena.client.event.ProduceListChangeEvent;
import com.lenin.omena.client.event.ProduceListChangeEventHandler;
import com.lenin.omena.client.event.SaveProduceEvent;
import com.lenin.omena.client.event.SaveProduceEventHandler;
import com.lenin.omena.shared.AppleMarketServiceAsync;
import com.lenin.omena.shared.SaveProduceResult;
import com.lenin.omena.shared.domain.Produce;
import com.lenin.omena.shared.domain.User;

public class ProduceRow implements SaveProduceEventHandler, ProduceListChangeEventHandler {
	
	
	private Produce produce;
	
	private Label produceNameField;

	private TextBox produceAmountField;
	
	private TextBox producePriceField;
	
	private Button deleteButton;
	
	private Button saveButton;
	
	
	public ProduceRow(Produce produce) {
		
		this.produce = produce;
		
		SaveProduceEvent.register(AppleMarket.getEventBus(), this);
		ProduceListChangeEvent.register(AppleMarket.getEventBus(), this);
		
		produceNameField = new Label();
		produceNameField.setWidth("100px");
		
		produceAmountField = new TextBox();
		produceAmountField.setWidth("50px");
		produceAmountField.setMaxLength(12);
		
		producePriceField = new TextBox();
		producePriceField.setWidth("50px");
		producePriceField.setMaxLength(16);
		
		setFieldValues();
		
		deleteButton = new Button("Poista");
		
		deleteButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				deleteProduce();
			}
		});
		
		saveButton = new Button("Tallenna");
		
		saveButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				saveProduce();
			}
		});
		
	
	}
	
	
	public void setFieldValues() {
		
		if(produce != null) {
			produceNameField.setText(produce.getName());
			produceAmountField.setText(produce.getAmount());
			producePriceField.setText(produce.getPrice());
		}
		
	}
	
	public Produce getProduce() {
		return produce;
	}
	
	public void setProduce(Produce produce) {
		this.produce = produce;
	}
	
	
	public Label getProduceNameField() {
		return produceNameField;
	}

	public TextBox getProduceAmountField() {
		return produceAmountField;
	}

	public TextBox getProducePriceField() {
		return producePriceField;
	}

	public Button getDeleteButton() {
		return deleteButton;
	}
	
	public Button getSaveButton() {
		return saveButton;
	}

	private void deleteProduce() {
		
		saveButton.setEnabled(false);
		deleteButton.setEnabled(false);
		
		AppleMarket.doDeleteProduce(AppleMarket.getUserContext().getUser(), produce);
		
	}
	
	@Override
	public void onProduceListChange(ProduceListChangeEvent event) {
		
		saveButton.setEnabled(true);
		deleteButton.setEnabled(true);
		
	}
	
	
	private void saveProduce() {
		
		saveButton.setEnabled(false);
		deleteButton.setEnabled(false);
		
		produce.setName(produceNameField.getText());
		produce.setAmount(produceAmountField.getText());
		produce.setPrice(producePriceField.getText());
		
		AppleMarketServiceAsync service = AppleMarket.getService();
		
		service.saveProduce(produce, new AsyncCallback<SaveProduceResult>() {
			
			public void onSuccess(SaveProduceResult result) {
				AppleMarket.getEventBus().fireEvent(new SaveProduceEvent(result));
			}
			
			public void onFailure(Throwable caught) {
				AppleMarket.getEventBus().fireEvent(new SaveProduceEvent(new SaveProduceResult(null, false)));
			}
			
		});
		
	}
	
	
	@Override
	public void onSaveProduce(SaveProduceEvent event) {
		
		saveButton.setEnabled(true);
		deleteButton.setEnabled(true);
	
	}

}
